<?php
namespace app\admin\controller;
use app\admin\model\Sysconfig;
use app\admin\model\Qi;
use app\admin\model\Qideatils;

class Game extends Base
{
    /**
     * 开奖记录
     * @return \think\response\View
     */
    public function index()
    {
        $nowpage = I('param.page');
        $ispage=true;
        $data = input();
        $where = [];
        if(!empty($data['sci'])) {
            $ispage=false;
            $nowpage=1;
            $where['name'] = $data['sci'];
        }
        if(!$nowpage){
            $nowpage=1;
        }
        if(!empty($data['sktime']) && !empty($data['sjtime'])) {
            $ispage=false;
            $nowpage=1;
            $time = [];
            $time[] = $data['sktime'];
            $time[] = $data['sjtime'];
            $where['create_time'] = ['between',$time];
        }
        
        $qi = new Qi();
        if ($ispage){
            $list = $qi->where($where)->page($nowpage,10)->order("create_time desc")->select();
        }else {
            $list = $qi->where($where)->order("create_time desc")->select();
        }
        
        $num = count($list);
        $this->assign('list',$list);
        $prepage=$nowpage-1;
        if ($nowpage==1){
            $prepage=0;
        }
        $this->assign("prepage",$prepage);
        $this->assign("page",$num);
        $this->assign("nowpage",$nowpage);
        $nextpage=$nowpage+1;
        if ($num<10){
            $nextpage = 0;
        }
        $this->assign("nextpage",$nextpage);
        return view();
    }

    /**
     * 每期详情
     */
    public function qidetail()
    {
        $data = input();
        $qd = new Qideatils();
        $list = $qd->alias('q')
                   ->field('q.type,q.user_id,q.xiazhu_type,q.peilv,q.money,u.nickname,i.name')
                   ->join('user u','q.user_id=u.id')
                   ->join('qi i','q.qi_id=i.id')
                   ->where('q.qi_id',$data['id'])
                   ->select();
        $this->assign('list',$list);
        return view();
    }

    /**
     * 游戏设置
     * @return \think\response\View
     */
    public function setting()
    {
        $sys = new Sysconfig();
        $list = $sys->find();
        $this->assign('list',$list);
        return view();
    }
    /**
     * 游戏设置
     * @return \think\response\View
     */
    public function setting2()
    {
        $sys = new Sysconfig();
        $list = $sys->find();
        $this->assign('list',$list);
        return view();
    }
    
    function ajaxsetting2(){
        $state = I("param.states");
        $sys = D("sysconfig");
        $list = $sys->find();
        if (!$state||$state==0){
            $list['is_close']=0;
        }else{

            $list['is_close']=1;
        }
        $sys->update($list);
        return $this->returnOk($list['is_close']);
    }
    /**
     * 添加游戏配置
     */
    public function addSet()
    {
        $data = input();
        $sys = D('sysconfig');
        if(empty($data['id'])) {
            //新增
            $tmp['max_value'] = $data['maxmoney']*100;
            $tmp['min_value'] = $data['minmoney']*100;
            $tmp['game_time'] = $data['gametime'];
            $sys->insert($tmp);
        }else {
            //更新
            $tmp['id'] = $data['id'];
            $tmp['max_value'] = $data['maxmoney']*100;
            $tmp['min_value'] = $data['minmoney']*100;
            $tmp['game_time'] = $data['gametime'];
            $sys->update($tmp);
        }
        $jccount = new \app\index\model\Jccount();
        $jccount->setSYSTime($data['gametime']);
            $this->redirect('Game/setting');
    }

    /**
     * 正在进行
     * @return \think\response\View
     */
    function running() {
        $jccount = new \app\index\model\Jccount();
        $res = $jccount->getLock();
        $jc = D('qi');
        $qiinfo = $jc->where("status",0)->where("id",$res['id'])->find();
        $this->assign('qiinfo',$qiinfo);
        $this->assign('pushhost',config('pushhost'));
        return view();
    }
    
    function settingsave() {
        
    }
}
